# -*- coding:utf-8 -*-
from Trajectories import *
from Volumes import *
from Weathers import *
from pre_processing import *
from Routes import *
import datetime
import numpy as np

def get(mode,h):
    p=-100
    if mode==0 and h in [5,6,7,8]:
        p=h-6
    if mode==1 and h in [7,8,9,10]:
        p=h-8
    if mode==2 and h in [14,15,16,17]:
        p=h-15
    if mode==3 and h in [16,17,18,19]:
        p=h-17
    return p

'''
    对于每一个小段分别生成数据
    输入
    6-8、15-17两个时间段分开
    6维*若干，该段和相邻段的车通过时间维度
    6、15两个时刻5个天气维度
    7个星期维度
    输出
    8-10、17-19两个时间段分开
    6个维该段的车通过的时间维度
    无2016-10-10天气信息
    无2016-09-29的21时的天气信息
    去掉2016.9.15~17中秋(18日调休工作)，2016.10.1~7国庆(8、9调休工作)，G20峰会2016.9.3~5
'''
def buildTrajectories(trajectories,mode,linklist):
    tra=[]
    num=[]
    d=trajectories.available_dates()
    l=len(d)
    ll=len(linklist)
    for i in range(0,l):
        line=[0.0]*(6*ll)
        tra.append(line)
        line=[0.0]*(6*ll)
        num.append(line)
    for i in trajectories.trajectories:
        t=i.starting_time
        tt=datetime.date(t.year,t.month,t.day)
        for j in range(0,l):
            if d[j]==tt:
                for k in i.travel_seq:
                    t=k.time
                    p=get(mode,t.hour)
                    p=p*3+t.minute/20
                    q=linklist.index(k.seq_id)
                    for o in range(0,1):
                        pp=p+o
                        ss=0.1**abs(o)
                        if pp>=0 and pp<6:
                            pp+=q*6
                            tra[j][pp]+=k.tot_time*ss
                            num[j][pp]+=ss
    for i in range(0,l):
        for j in range(0,6*ll):
            if num[i][j]>0:
                tra[i][j]/=num[i][j]
    return tra

def check(rec):
    if rec.month==8 and rec.day>=29 and rec.day<=31:
        return False
    if rec.month==9 and rec.day>=1 and rec.day<=5:
        return False
    if rec.month==9 and rec.day>=15 and rec.day<=18:
        return False
    if rec.month==10 and rec.day>=1 and rec.day<=9:
        return False
    return True

def buildWeathers(weathers):
    wea=[]
    days=[]
    line=[]
    s=0
    for i in weathers.weathers:
        if i.hour==0:
            line=[]
            s=0
        if i.hour in [3,6,9,15,18,21]:
            s+=1
            line.extend([i.pressure,i.wind_speed,i.temperature,i.rel_humidity,i.precipitation])
        if i.hour==21 and s==6 and check(i.rec_date):
            wea.append(line)
            days.append(i.rec_date)
        if i.hour>=21:
            line=[]
            s=0
    return wea,days

def buildinput(tra,tra2,mode,days,wea,weadays,links):
    l1=len(tra)
    l2=len(wea)
    datas=[]
    for i in range(0,l1):
        for j in range(0,l2):
            if days[i]==weadays[j]:
                line=[]
                for link in links:
                    line.extend(tra[i][link*6:link*6+6])
                    if mode==1:
                        line.extend(tra2[i][link*6:link*6+6])
                line.extend(wea[j])
                o=days[i].weekday()
                ll=[0]*7
                ll[o]=1
                line.extend(ll)
                datas.append(line)
    print 'input data number:',len(datas),'dimension number:',len(datas[0])
    return datas

def buildoutput(tra,days,weadays,link):
    l=len(tra)
    labels=[]
    for i in range(0,l):
        if days[i] in weadays:
            line=[]
            line.extend(tra[i][link*6:link*6+6])
            labels.append(line)
    print 'output label number:',len(labels),'dimension number:',len(labels[0])
    return labels
    
if __name__ == '__main__':
    routes=Routes()
    links=Links()
    readRoutes(routes)
    readLinks(links)
    linklist=routes.get_links()
    print linklist
    
    trajectories=Trajectories()
    weathers=Weathers()
    trajectories2=Trajectories()
    weathers2=Weathers()
    
    readTrajectories(trajectories)
    readWeathers(weathers)
    readTrajectories2(trajectories2)
    readWeathers2(weathers2)
    
    (wea,weadays)=buildWeathers(weathers)
    days=trajectories.available_dates()
    days2=trajectories2.available_dates()
    
    tra0=buildTrajectories(trajectories,0,linklist)
    tra1=buildTrajectories(trajectories,1,linklist)
    tra2=buildTrajectories(trajectories,2,linklist)
    tra3=buildTrajectories(trajectories,3,linklist)

    (weatest,weatestdays)=buildWeathers(weathers2)
    tratest0=buildTrajectories(trajectories2,0,linklist)
    tratest2=buildTrajectories(trajectories2,2,linklist)

    for i in range(0,len(linklist)):
        print 'build data of link',linklist[i]
        g=links.find_link(linklist[i]).get_neighbor()
        h=[]
        for j in g:
            h.append(linklist.index(j))
        datas=buildinput(tra0,tra2,0,days,wea,weadays,h)
        labels=buildoutput(tra1,days,weadays,i)
        tests=buildinput(tratest0,tratest2,0,days2,weatest,weatestdays,h)
        np.savez('./data_links/traindata'+str(linklist[i])+'_part0',datas=np.array(datas),labels=np.array(labels))
        np.savez('./data_links/testdata'+str(linklist[i])+'_part0',tests=np.array(tests))
        datas=buildinput(tra0,tra2,1,days,wea,weadays,h)
        labels=buildoutput(tra3,days,weadays,i)
        tests=buildinput(tratest0,tratest2,1,days2,weatest,weatestdays,h)
        np.savez('./data_links/traindata'+str(linklist[i])+'_part1',datas=np.array(datas),labels=np.array(labels))
        np.savez('./data_links/testdata'+str(linklist[i])+'_part1',tests=np.array(tests))

    
